/*--------------------------------------------------------------------------------*/
#ifndef __GRAPHIC_TEXTURE_SET__
#define __GRAPHIC_TEXTURE_SET__
/*--------------------------------------------------------------------------------*/
#include "primitives.h"
#include "object.h"
#include "util.h"
#include "debug.h"
#include "graphic_texture.h"
#include "array.h"
#include "math_vector_2.h"
/*--------------------------------------------------------------------------------*/
#define GRAPHIC_ATLAS GRAPHIC_TEXTURE_SET
/*--------------------------------------------------------------------------------*/
class GRAPHIC_TEXTURE_SET : public GRAPHIC_TEXTURE
{

	// -- PUBLIC

public:

    DECLARE_REF( GRAPHIC_TEXTURE_SET );

    // .. TYPES

    struct UV_COORD
    {
    	MATH_VECTOR_2
			Start,
			End;
    };

    // .. CONSTRUCTORS

    GRAPHIC_TEXTURE_SET() : GRAPHIC_TEXTURE(), UVTable(), RowCount( 0 ), ColumnCount( 0 )
    {

    }

    virtual ~GRAPHIC_TEXTURE_SET()
    {
    }

    // .. METHODS

    VOID SetRowCount( const COUNT row_count )
    {
    	RowCount = row_count;
    }

    VOID SetColumnCount( const COUNT column_count )
    {
    	ColumnCount = column_count;
    }

    VOID ComputeUVTable()
    {
    	INDEX
			row_index,
			col_index;
    	REAL
			row_factor,
			col_factor;

    	DEBUG_Require( RowCount && ColumnCount );

    	row_factor = 1.0f / RowCount;
    	col_factor = 1.0f / ColumnCount;

    	UVTable.SetItemCount( RowCount * ColumnCount );
    	UVTable.Clear();

    	for_each_index( row_index, RowCount )
    	{
        	for_each_index( col_index, ColumnCount )
        	{
				UV_COORD
					coord;

				coord.Start.Set( col_factor * col_index, 1.0f - row_factor * ( row_index + 1 ) );
				coord.End.Set( col_factor * ( col_index + 1 ), 1.0f - row_factor * ( row_index ) );

				// :TODO: Check if offset is needed.

				UVTable.Add( coord );
        	}
    	}
    }

    const UV_COORD & GetUVCoord( const INDEX col, const INDEX row ) const
    {
    	return GetUVCoord( col + row * ColumnCount );
    }

    const UV_COORD & GetUVCoord( const INDEX index ) const
    {
    	return UVTable[ index ];
    }

    // .. FUNCTIONS

    static GRAPHIC_TEXTURE_SET::REF CreateTextureSetFromFile( const TEXT & file_name, const COUNT column_count, const COUNT row_count )
    {
    	GRAPHIC_TEXTURE_SET::REF
			result = NULL;

    	result.New();
    	result->LoadTextureFromFile( file_name );
    	result->SetColumnCount( column_count );
    	result->SetRowCount( row_count );
    	result->ComputeUVTable();

    	return result;
    }

    // -- PRIVATE

private :

    // .. ATTRIBUTES

    ARRAY< UV_COORD >
		UVTable;
    COUNT
		RowCount,
		ColumnCount;

    
};
/*--------------------------------------------------------------------------------*/
#endif
